﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotDealD2.Models.ModelView;
using HotDealD2.Models.ModelManager;
using HotDealD2.Models;

namespace HotDealD2.Controllers
{
    [Authorize(Roles="Admin, Business, Member")]
    public class MemberController : Controller
    {
        //
        // GET: /Member/

        public ActionResult Index()
        {
            AccountModel accountmodel = new AccountModel();
            ThanhVien thanhvien = accountmodel.GetAccount(Session["Logon"].ToString());
            return View(thanhvien);
        }

        public ActionResult DonHang()
        {

           
            OrderDealModel ordermodel = new OrderDealModel();
            IList<OrderDealView> listDonHang = ordermodel.GetListOrderPromotionByUsername(Session["Logon"].ToString());
            return View(listDonHang);
        }

        public ActionResult ThanhToan()
        {
            ShoppingCartOrderDealView shopingorderview = new ShoppingCartOrderDealView();
            ShoppingCartView shoppingcartindex = new ShoppingCartView();
            int Price = 0;
            var list = new List<String>();
            CartModel Cart = GetCart();
            IList<CartView> cartview = Cart.CartView;
            for (int i = 0; i < cartview.Count; i++)
            {
                Price += cartview[i].PriceItem;
                list.Add(String.Format("{0:0,0}", cartview[i].deal.GiaTien));
                String.Format("{0:0,0}", cartview[i].PriceItem);
            }

            ViewBag.GiaTien = list;
            ViewBag.TongTien = String.Format("{0:0,0}", Price);
            object username = "member";
            shoppingcartindex.CartModel = Cart;
            shopingorderview.ShoppingCartView = shoppingcartindex;

            return View(shopingorderview);
        }
        [HttpPost]
        public ActionResult ThanhToan(ShoppingCartOrderDealView shoppingorderview,FormCollection form)

        {

            ShoppingCartOrderDealView shoppingorderviewtemp = new ShoppingCartOrderDealView();
            ShoppingCartView shoppingcartindex = new ShoppingCartView();
            if (ModelState.IsValid)
            {
                int type = 0;
                if(form.Get("txttypepayment")!=null)
                    type = Int32.Parse(form.Get("txttypepayment").ToString());
                OrderDealModel orderdealmodel = new OrderDealModel();
                CartModel Cart = GetCart();
                if (type == 0)
                    shoppingorderview.OrderDealView.HinhThucThanhToan = "Thanh Toán Trực Tiếp";
                if (type == 2)
                    shoppingorderview.OrderDealView.HinhThucThanhToan = "Paypal";
                if (type == 1)
                    shoppingorderview.OrderDealView.HinhThucThanhToan = "Thanh Toán Trực Tiếp";
                shoppingorderview.OrderDealView.TinhThanh = Int32.Parse(form.Get("tinh").ToString());
                shoppingorderview.OrderDealView.QuanHuyen = Int32.Parse(form.Get("quan").ToString());
                shoppingorderview.OrderDealView.ThanhVien.Username = Session["LogOn"].ToString();
                bool result = orderdealmodel.InsertOrder(shoppingorderview, Cart);
                if (result)
                {
                    return RedirectToAction("CompleteCart", "Member");
                    
                }
            }
            else
            {
                int Price = 0;
                var list = new List<String>();
                CartModel Cart = GetCart();
                IList<CartView> cartview = Cart.CartView;
                for (int i = 0; i < cartview.Count; i++)
                {
                    Price += cartview[i].PriceItem;
                    list.Add(String.Format("{0:0,0}", cartview[i].deal.GiaTien));
                    String.Format("{0:0,0}", cartview[i].PriceItem);
                }

                ViewBag.GiaTien = list;
                ViewBag.TongTien = String.Format("{0:0,0}", Price);
                shoppingcartindex.CartModel = Cart;
                shoppingorderviewtemp.ShoppingCartView = shoppingcartindex;
            }
            return View(shoppingorderviewtemp);

        }

        public ActionResult ShoppingCart()
        {
            ShoppingCartView shoppingcartindex = new ShoppingCartView();
            int Price =0;
            var list = new List<String>();
            CartModel Cart = GetCart();
            IList<CartView> cartview = Cart.CartView;
            for(int i = 0;i< cartview.Count;i++)
            {
                Price += cartview[i].PriceItem;
                list.Add(String.Format("{0:0,0}", cartview[i].deal.GiaTien));
                String.Format("{0:0,0}", cartview[i].PriceItem);
            }
            
            ViewBag.GiaTien = list;
            ViewBag.TongTien = String.Format("{0:0,0}", Price);
            object username = "member";
            shoppingcartindex.CartModel = Cart;

            return View(shoppingcartindex);
        }
        public RedirectToRouteResult RemoveFromCart( int maDeal) 
        {
            
            DealModel dealmodel = new DealModel();
            Deal deal = dealmodel.GetDeal(maDeal);
            if (deal != null) 
            {
                GetCart().RemoveLine(deal);
            }
            return RedirectToAction("ShoppingCart", "Member");
        }
        public ActionResult CompleteCart()
        {
            ShoppingCartView shoppingcartindex = new ShoppingCartView();
            int Price = 0;
            var list = new List<String>();
            CartModel Cart = GetCart();
            IList<CartView> cartview = Cart.CartView;
            for (int i = 0; i < cartview.Count; i++)
            {
                Price += cartview[i].PriceItem;
                list.Add(String.Format("{0:0,0}", cartview[i].deal.GiaTien));
                String.Format("{0:0,0}", cartview[i].PriceItem);
            }

            ViewBag.GiaTien = list;
            ViewBag.TongTien = String.Format("{0:0,0}", Price);
            object username = "member";
            shoppingcartindex.CartModel = Cart;

            return View(shoppingcartindex);
            Session["Cart" + Session["LogOn"]] = null;
            return View(shoppingcartindex);
        }

        public ActionResult DoiMatKhau()
        {
            object username = "member";

            return View(username);
        }
        public CartModel GetCart()
        {
            CartModel cart = (CartModel)Session["Cart" + Session["Logon"]];
            if (cart == null)
            {
                cart = new CartModel();
                Session["Cart" + Session["Logon"]] = cart;
            }
            return cart;

        }


    }
}
